<?php
namespace app\admin\controller;

use app\admin\model\Advert as AdvertModel;
use think\Request;

class Advert extends Base
{
    protected $model;
    protected $advert_couplet_model;
    protected $advert_couplet_window;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->model = new AdvertModel();
    }

    public function index()
    {
        $map = [];

        // if ($search) {
        //     $map['title'] = ['like','%'.$search.'%'];

        //     $this->assign('search', $search);
        // }

        // if ($isshow) {
        //     $map['isshow'] = $isshow;

        //     $this->assign('isshow', $isshow);
        // }

        $data = $this->model->where($map)
            ->field('id, typeid, title, thumb, link_url, sort, status, mtime')
            ->select();

        $this->assign('data', $data);
        $this->assign('data_type', $this->model->advert_type);
        return $this->fetch();
    }

    public function edit($id)
    {
        $data = $this->model->find($id);
        if (Request::instance()->isPost()) {
            $param = input('post.');

            $data->title = $param['title'];
            $data->link_url = $param['link_url'];
            $data->sort = $param['sort'];
            $data->mtime = time();

            $test = $data->save();

            if (!$test) {
                $this->error('保存出错');
            }

            $this->success('保存成功', 'index');
        } else {
            $this->assign('data', $data);

            return $this->fetch();
        }
    }

    public function advertAdd()
    {
        if (Request::instance()->isPost()) {
            $param = input('post.');
            $this->model->typeid = 0;

            $this->model->title = $param['title'];
            $this->model->link_url = $param['link_url'];
            $this->model->sort = $param['sort'];
            $this->model->create_time = time();

            $test = $this->model->save();

            if (!$test) {
                $this->error('保存出错');
            }

            $this->success('保存成功', '/admin/advert/adindex/');
        } else {
            return $this->fetch();
        }
    }

    public function del($id)
    {
        if (Request::instance()->isAjax()) {
            if ($this->model->destroy($id)) {
                return ['code'=>2, 'msg'=>"删除成功"];
            } else {
                return ['code'=>0, 'msg'=>"删除出错"];
            }
        }
    }

    public function adFixed($search = null, $isshow = null)
    {
        $typeid = 1;
        $map = array();

        if ($search) {
            $map['title'] = ['like','%'.$search.'%'];

            $this->assign('search', $search);
        }

        if ($isshow) {
            $map['isshow'] = $isshow;

            $this->assign('isshow', $isshow);
        }

        $advert_index = $this->model->where('typeid', $typeid)->where($map)->order('id', 'desc')->select();

        $this->assign('data', $advert_index);

        return $this->fetch('adindex');
    }

    public function adCouplet()
    {
        $id = 1;
        $data = $this->advert_couplet_model->find($id);

        if (Request::instance()->isPost()) {
            $param = input('post.');
         
            if (input('post.isshow')) {
                $data->isshow = input('post.isshow') ;
            } else {
                $data->isshow = 0;
            }
            $data->left_link = $param['left_link'];
            $data->right_link = $param['right_link'];
            $data->interval_top = $param['interval_top'];
            $data->interval_lr = $param['interval_lr'];
          
            $test = $data->save();
            if (!$test) {
                $this->error('保存出错');
            }

            $this->success('保存成功', '/admin/advert/adcouplet');
        } else {
            $this->assign('data', $data);

            return $this->fetch();
        }
    }

    /**
     * adFloat 漂浮广告
     *
     * 数据表使用的hjzs_advert_couplet
     * 使用 left_file left_link 表示图片路径和url地址
     * @return void
     */
    public function adFloat()
    {
        $id = 2;
        $data = $this->advert_couplet_model->find($id);
        
        if (Request::instance()->isPost()) {
            $param = input('post.');
           
            if (input('post.isshow')) {
                $data->isshow = input('post.isshow') ;
            } else {
                $data->isshow = 0;
            }
            $data->left_link = $param['left_link'];
            $data->interval_top = $param['interval_top'];
            $data->interval_lr = $param['interval_lr'];
          
            $test = $data->save();
            if (!$test) {
                $this->error('保存出错');
            }

            $this->success('保存成功', '/admin/advert/adfloat');
        } else {
            $this->assign('data', $data);
    
            return $this->fetch();
        }
    }

    public function adNewWindow()
    {
        $id = 1;
        $data = $this->advert_couplet_window->find($id);

        if (Request::instance()->isPost()) {
            $param = input('post.');
            if (input('post.isshow')) {
                $data->isshow = input('post.isshow') ;
            } else {
                $data->isshow = 0;
            }

            $data->title = $param['title'];
            $data->window_width = $param['window_width'];
            $data->window_height = $param['window_height'];
            $data->interval_top = $param['interval_top'];
            $data->interval_left = $param['interval_left'];
            if (input('post.editorValue')) {
                $data->content = input('post.editorValue');
            } else {
                $data->content = '';
            }
            
            $test = $data->save();
            if (!$test) {
                $this->error('保存出错');
            }

            $this->success('保存成功', '/admin/advert/adnewwindow');
        } else {
            $this->assign('data', $data);
    
            return $this->fetch();
        }
    }
}
